import React, { useEffect } from 'react'
import {Table,Button,notification} from 'antd'
import axios from 'axios'


export default function Published() {
  const {username}=JSON.parse(localStorage.getItem('token'))
  const [state,SetState]=React.useState([])
 useEffect(()=>{
    axios.get(`http://localhost:8000/news?author=${username}&publishState=2&_expand=category`).then(res=>{
      SetState(res.data)
    })
  },[])
  console.log(state,"state");
  const columns = [
    {
      title: '新闻标题',
      dataIndex: 'title',
      render:(title,item)=>{
        return<a href={`/news-manage/preview/${item.id}`}>{title}</a>
      }
    },
    {
      title: '作者',
      dataIndex: 'author',
    },
    {
      title: '新闻分类',
      dataIndex: 'categoryId',
      render: (categoryId) => {
        return <div>{categoryId}</div>
    }
    },
    {
      title: '操作',
      render:(item)=>{
        return <div>
      <Button type='primary'  onClick={()=>handleAudit(item,3)}>下线</Button>
        </div>  
      }
    },
  ];
  const handleAudit=(item,publishState)=>{
    SetState(state.filter(data=>data.id!==item.id))
    axios.patch(`http://localhost:8000/news/${item.id}`,{
      publishState
  }).then(res=>{
      notification.info({
          message: `通知`,
          description:
            `您可以到[发布管理/已发布列表]中查看您的新闻的审核状态`,
          placement:"bottomRight"
      });
  })
}
  return (
    <div>
    <Table dataSource={state} columns={columns} pagination={{ pageSize:5}} rowKey={item=>item.id}/>
  </div>
  )
}
